package com.jiermt.M0615;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ListDemo {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("1");
        list.add("2");
        list.add("3");
        list.add("4");
        list.add("5");
        System.out.println(list);
        System.out.println("-----------------");
        list.add(1, "增加");
        System.out.println(list);

        // 修改元素
        list.set(1, "修改");
        System.out.println(list);

        list.remove(1);

        System.out.println("下面是四种遍历");

        // for循环
        System.out.println("for循环");
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }

        // 迭代器
        System.out.println("迭代器");
        Iterator<String> it = list.iterator();
        while (it.hasNext()) {
            String next = it.next();
            System.out.println(next);
        }

        // 增强for循环
        System.out.println("增强for循环");
        for (String s : list) {
            System.out.println(s);
        }

        // lambda表达式
        System.out.println("lambda表达式");


        list.forEach(s -> System.out.println(s));
        list.forEach(System.out::println);
        System.out.println("-----------------");

        System.out.println("--------Lambda表达式的特殊处理--------");
        list.forEach(s -> {
            System.out.println(s);
        });

        System.out.println("--------简化Lambda--------");
        list.forEach((s -> System.out.println(s)));

        System.out.println("--------简化Lambda方法引用形式--------");
        list.forEach(System.out::println);
    }
}
